Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ios fabric transform origin #38559

Conversation

intergalacticspacehighway
Copy link
Contributor

@intergalacticspacehighway intergalacticspacehighway commented Jul 23, 2023

Summary:

This PR adds transform-origin support for iOS fabric. This PR also incorporates feedback/changes suggested by @javache in the original PR.

Changelog:

[IOS] [ADDED] - Fabric Transform origin

Test Plan:

Run iOS RNTester app in old architecture and test transform-origin example in TransformExample.js.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jul 23, 2023
@github-actions
Copy link

github-actions bot commented Jul 23, 2023

Warnings
⚠️

packages/react-native/Libraries/Components/View/ReactNativeStyleAttributes.js#L11 - packages/react-native/Libraries/Components/View/ReactNativeStyleAttributes.js line 11 – Requires should be sorted alphabetically (lint/sort-imports)

Generated by 🚫 dangerJS against a30ed3d

@facebook-github-bot facebook-github-bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Jul 23, 2023
@analysis-bot
Copy link

analysis-bot commented Jul 23, 2023

Platform Engine Arch Size (bytes) Diff
android hermes arm64-v8a 8,962,403 +2,327
android hermes armeabi-v7a n/a --
android hermes x86 n/a --
android hermes x86_64 n/a --
android jsc arm64-v8a 9,556,421 +2,331
android jsc armeabi-v7a n/a --
android jsc x86 n/a --
android jsc x86_64 n/a --

Base commit: a0a544f
Branch: main

@intergalacticspacehighway intergalacticspacehighway changed the title feat(ios(fabric)): transform origin feat: ios fabric transform origin Jul 27, 2023
@intergalacticspacehighway
Copy link
Contributor Author

Getting this error on test_android CI 🤔

C/C++: /root/react-native/packages/react-native/ReactCommon/react/renderer/graphics/Transform.h:19:10: fatal error: 'yoga/YGValue.h' file not found

@facebook-github-bot
Copy link
Contributor

@rozele has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

Copy link
Member

@javache javache left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for splitting these up into multiple PR's!

@@ -727,6 +727,7 @@ export type ____ViewStyle_InternalCore = $ReadOnly<{
opacity?: AnimatableNumericValue,
elevation?: number,
pointerEvents?: 'auto' | 'none' | 'box-none' | 'box-only',
transformOrigin?: string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this required? transformOrigin should be included via ____TransformStyle_Internal already.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My bad. Updated!

Comment on lines 19 to 44
namespace {

std::array<float, 3> getTranslateForTransformOrigin(float viewWidth, float viewHeight, facebook::react::TransformOrigin transformOrigin) {
float viewCenterX = viewWidth / 2;
float viewCenterY = viewHeight / 2;

std::array<float, 3> origin = {viewCenterX, viewCenterY, transformOrigin.z};

for (size_t i = 0; i < 2; ++i) {
auto& currentOrigin = transformOrigin.xy[i];
if (currentOrigin.unit == YGUnitPoint) {
origin[i] = currentOrigin.value;
} else if (currentOrigin.unit == YGUnitPercent) {
origin[i] = ((i == 0) ? viewWidth : viewHeight) * currentOrigin.value / 100.0f;
}
}


float newTranslateX = -viewCenterX + origin[0];
float newTranslateY = -viewCenterY + origin[1];
float newTranslateZ = origin[2];

return std::array{newTranslateX, newTranslateY, newTranslateZ};
}

}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: move inside the namespace facebook::react below (and remove the extra facebook::react prefixes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

Comment on lines 566 to 587
TransformOrigin transformOrigin;

for (size_t i = 0; i < 2; i++) {
const auto& origin = origins[i];
if (origin.hasType<Float>()) {
transformOrigin.xy[i] = yogaStyleValueFromFloat((Float)origin);
} else if (origin.hasType<std::string>()) {
const auto stringValue = (std::string)origin;

if (stringValue.back() == '%') {
auto tryValue = folly::tryTo<float>(
std::string_view(stringValue).substr(0, stringValue.length() - 1));
if (tryValue.hasValue()) {
transformOrigin.xy[i] = YGValue{tryValue.value(), YGUnitPercent};
}
}
}
}

if (origins[2].hasType<Float>()) {
transformOrigin.z = (Float)origins[2];
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You do need to check now that the indices you're accessing are not exceeding the vector bounds.

Suggested change
TransformOrigin transformOrigin;
for (size_t i = 0; i < 2; i++) {
const auto& origin = origins[i];
if (origin.hasType<Float>()) {
transformOrigin.xy[i] = yogaStyleValueFromFloat((Float)origin);
} else if (origin.hasType<std::string>()) {
const auto stringValue = (std::string)origin;
if (stringValue.back() == '%') {
auto tryValue = folly::tryTo<float>(
std::string_view(stringValue).substr(0, stringValue.length() - 1));
if (tryValue.hasValue()) {
transformOrigin.xy[i] = YGValue{tryValue.value(), YGUnitPercent};
}
}
}
}
if (origins[2].hasType<Float>()) {
transformOrigin.z = (Float)origins[2];
}
TransformOrigin transformOrigin;
for (size_t i = 0; i < std::min(origins.size(), 2); i++) {
const auto& origin = origins[i];
if (origin.hasType<Float>()) {
transformOrigin.xy[i] = yogaStyleValueFromFloat((Float)origin);
} else if (origin.hasType<std::string>()) {
const auto stringValue = (std::string)origin;
if (stringValue.back() == '%') {
auto tryValue = folly::tryTo<float>(
std::string_view(stringValue).substr(0, stringValue.length() - 1));
if (tryValue.hasValue()) {
transformOrigin.xy[i] = YGValue{tryValue.value(), YGUnitPercent};
}
}
}
}
if (origin.size() >= 3 && origins[2].hasType<Float>()) {
transformOrigin.z = (Float)origins[2];
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated!

@@ -50,6 +51,23 @@ struct TransformOperation {
Float z;
};

struct TransformOrigin {
std::array<YGValue, 2> xy;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per @NickGerleman feedback on another PR, using YGValue is an undesired dependency. Let's introduce a custom struct for this in another PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a PR for struct - #39150. I have tested it replacing YGValue and it works, kept it simple. let me know!

std::array<YGValue, 2> xy; -> std::array<ValueUnit, 2> xy;

cc - @jacobp100 we can also use it here - #38626

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will try and get to it later this weekend


namespace facebook::react {

enum class UnitType {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need undefined?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To represent initial/undefined value? Also useful to check the value is set. Check isSet in TransformOrigin struct.

@facebook-github-bot
Copy link
Contributor

@javache has imported this pull request. If you are a Meta employee, you can view this diff on Phabricator.

javache pushed a commit to javache/react-native that referenced this pull request Sep 1, 2023
Summary:
This PR adds transform-origin support for iOS fabric. This PR also incorporates feedback/changes suggested by javache in the original [PR.](facebook#37606)

## Changelog:
[IOS] [ADDED] - Fabric Transform origin
<!-- Help reviewers and the release process by writing your own changelog entry.

Pick one each for the category and type tags:

[ANDROID|GENERAL|IOS|INTERNAL] [BREAKING|ADDED|CHANGED|DEPRECATED|REMOVED|FIXED|SECURITY] - Message

For more details, see:
https://reactnative.dev/contributing/changelogs-in-pull-requests

Pull Request resolved: facebook#38559

Test Plan: Run iOS RNTester app in old architecture and test transform-origin example in `TransformExample.js`.

Differential Revision: D48528363

Pulled By: javache

fbshipit-source-id: 21cfe06db750c8cf55d43039f0189089d29fca6f
javache pushed a commit to javache/react-native that referenced this pull request Sep 1, 2023
Summary:
Adds transform-origin for old arch iOS

See also intergalacticspacehighway's work for iOS Fabric and Android:-

- facebook#38559
- facebook#38558

## Changelog:

[IOS] [ADDED] - Added support for transform-origin on old arch

Pull Request resolved: facebook#38626

Test Plan: See RN tester

Differential Revision: D48528353

Pulled By: javache

fbshipit-source-id: 0189f374c9556b6593b3d72d7503b9cf166378c2
@javache
Copy link
Member

javache commented Sep 1, 2023

This is now ready to ship internally so should be pushed soon. Please also submit a PR to https://github.com/facebook/react-native-website to add documentation for this.

@jacobp100
Copy link
Contributor

@javache Sorry - I never found time to do the iOS old arch - do you still need help with that?

facebook-github-bot pushed a commit that referenced this pull request Sep 1, 2023
Summary:
Adds transform-origin for old arch iOS

See also intergalacticspacehighway's work for iOS Fabric and Android:-

- #38559
- #38558

## Changelog:

[IOS] [ADDED] - Added support for transform-origin on old arch

Pull Request resolved: #38626

Test Plan: See RN tester

Reviewed By: NickGerleman

Differential Revision: D48528353

Pulled By: javache

fbshipit-source-id: 09592411e26484d81a999a7e601eff751ca7ae0b
@facebook-github-bot
Copy link
Contributor

@javache merged this pull request in 177ef21.

@facebook-github-bot facebook-github-bot added the Merged This PR has been merged. label Sep 1, 2023
@javache
Copy link
Member

javache commented Sep 5, 2023

@javache Sorry - I never found time to do the iOS old arch - do you still need help with that?

Doesn't 5f40f08 handle the old arch? What are we missing still?

@jacobp100
Copy link
Contributor

@javache Sorry - I never found time to do the iOS old arch - do you still need help with that?

Doesn't 5f40f08 handle the old arch? What are we missing still?

I think the plan was to remove the dependency on Yoga in this - 5f40f08#diff-6654f56131a3cc2c701d1ca11fbdd2c4c41854136153eaceba4a103413e81809R11-R15

So make our own struct that stores the float value, and whether it was a percent or pixel value

@javache
Copy link
Member

javache commented Sep 5, 2023

I think the plan was to remove the dependency on Yoga in this - 5f40f08#diff-6654f56131a3cc2c701d1ca11fbdd2c4c41854136153eaceba4a103413e81809R11-R15

So make our own struct that stores the float value, and whether it was a percent or pixel value

I think we're ok as-is, as we've been able to cleanup the dependency in the new arch version.

@intergalacticspacehighway intergalacticspacehighway deleted the feat/ios-fabric-transformorigin branch August 9, 2024 22:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Merged This PR has been merged. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants